import Taro from '@tarojs/taro';
// import { toast, showModal } from '../utils/uni';

function toast(title, obj = { icon: 'success' }) {
  Taro.showToast({
    title,
    ...obj,
  });
}

function showModal(content) {
  Taro.showModal({
    content,
  });
}

/**
 * icon: 'none' 不显示图标，文本在小程序最多可显示两行
 * success/error 文本在小程序平台最多显示 7 个汉字长度，App 仅支持单行显示
 */
export const $toast = (message) => {
  toast(message, { icon: 'none' });
};

export const $showToast = (message) => {
  toast(message);
};

export const $toastSuccess = (message) => {
  toast(message);
};

export const $toastWarning = (message) => {
  toast(message, { icon: 'error' });
};

export const $showModal = (message) => {
  showModal(message);
};

const ERROR_MSG = {
  'requestPayment:fail cancel': '您已取消支付',
  'saveImageToPhotosAlbum:fail cancel': '您已取消保存',
};

export const $catchError = (err) => {
  let msg = err?.errMsg || err?.errorMessage || err?.msg || err?.message;

  if (Object.keys(ERROR_MSG).includes(msg)) {
    msg = ERROR_MSG[msg];
  }

  const hasCatchError = Taro.getStorageSync('has-catch-error');
  if (hasCatchError) {
    Taro.removeStorageSync('has-catch-error');
  } else if (msg) {
    toast(msg, { icon: 'error' });
  }
  console.log('catch err :', err);
};
